<?php

namespace app\common\model;

class Assistant extends \think\Model
{
    const CMS_AKEY = "CMS_AKEY";

    protected $visible = ['id', 'name', 'credit_id', 'class_name', 'phone', 'openid','college'];

    public function assistant()
    {
        return $this->belongsTo('Assistant');
    }

    public function courses()
    {
        return $this->hasMany('CourseAssistant', 'assistant_id');
    }

    public function dutyArrange()
    {
        return $this->hasMany('DutyArrange', 'assistant_id');
    }

    public function dutyRecords()
    {
        return $this->hasMany('DutyRecord', 'assistant_id');
    }

    /**
     * 获取此助理的AKey(用于签到)
     *
     * @return void
     */
    public function getAKey()
    {
        $hashids = new \Hashids\Hashids(Assistant::CMS_AKEY, 4);
        return $hashids->encode([$this->getAttr('id'), date('md')]);
    }

    /**
     * 利用AKey找到对应的助理
     *
     * @param [type] $akey
     * @return void
     */
    public static function getByAKey(string $akey)
    {
        $hashids = new \Hashids\Hashids(Assistant::CMS_AKEY, 4);
        $arr = $hashids->decode($akey);
        if ($arr && $arr[1] == date('md')) {
            return Assistant::get($arr[0]);
        } else {
            return null;
        }
    }
}
